package com.cayte.location;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

public class CayteLocationSimpleCallback {
	private long interval = 5000;

	private Context con;
	private CayteLocationHelper clHelper;
	private int limit = 0;

	/**
	 * 只从网络获取位置的Callback
	 * 
	 * @param con
	 */
	public CayteLocationSimpleCallback(Context con) {
		this.con = con.getApplicationContext();
		this.clHelper = new CayteLocationHelper(this.con);
	}

	/**
	 * 设置从网络获取位置时间间隔
	 * 
	 * @param interval
	 *            时间间隔
	 */
	public void setInterval(long interval) {
		if (interval < 3000)
			interval = 3000;
		this.interval = interval;
	}

	/**
	 * 设置最大获取次数
	 * 
	 * @param limit
	 *            次数
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	private void sleep() {
		long sleepTime = interval;
		int count = (int) (interval / 1000);
		for (int i = 0; i < count; i++) {
			if (sleepTime != interval) {
				return;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private OnGetSimpleLocation onGetSimpleLocation;

	public abstract interface OnGetSimpleLocation {
		public void onGet(int state, CayteLocation loc);
	}

	public void setOnGetSimpleLocation(OnGetSimpleLocation onGetSimpleLocation) {
		this.onGetSimpleLocation = onGetSimpleLocation;
	}

	private class GetSimpleTask extends
			AsyncTask<String, CayteLocation, String> {
		private Handler getHandler;

		public GetSimpleTask(Handler getHandler) {
			// TODO Auto-generated constructor stub
			this.getHandler = getHandler;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			int count = 0;
			isTaskRun = true;
			while (isTaskRun) {
				count++;
				onProgressUpdate(clHelper.getNetworkLocation(null));
				if (limit == count) {
					break;
				} else {
					sleep();
				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(CayteLocation... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			if (getHandler != null) {
				CayteLocation loc = values[0];
				if (loc == null)
					return;
				Message msg = getHandler.obtainMessage(7);
				msg.obj = loc;
				msg.arg1 = loc.state;
				getHandler.sendMessage(msg);
			}
		}
	}

	private GetSimpleTask getSimpleTask = null;
	private Handler getHandler = null;

	/**
	 * 开始线程从网络获取位置
	 * 
	 * @param l
	 *            监听
	 */
	@SuppressLint({ "HandlerLeak" })
	public void runGetSimpleTask(OnGetSimpleLocation l) {
		this.onGetSimpleLocation = l;
		cancelGetSimpleTask();
		getHandler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				// TODO Auto-generated method stub
				super.dispatchMessage(msg);
				if (msg.what == 7) {
					if (onGetSimpleLocation != null) {
						if (msg.arg1 == CayteLocationState.SUCCESS)
							onGetSimpleLocation.onGet(msg.arg1,
									(CayteLocation) msg.obj);
						else
							onGetSimpleLocation.onGet(msg.arg1, null);
					}
				}
			}
		};
		getSimpleTask = new GetSimpleTask(getHandler);
		getSimpleTask.execute();
	}

	private boolean isTaskRun = true;

	/**
	 * 停止线程
	 */
	public void cancelGetSimpleTask() {
		// TODO Auto-generated method stub
		if (getSimpleTask != null) {
			isTaskRun = false;
			getSimpleTask.cancel(true);
		}
		getHandler = null;
		getSimpleTask = null;
	}
}
